package com.cardbaobao.cardbabyclient.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 昵称修改
 * Created by zad
 * 2016/12/26 20:10
 */
public class NicknameUpdateActivity extends BaseActivity implements TextWatcher {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    /**
     * 完成
     */
    @BindView(R.id.id_tv_submit)
    TextView mTvSubmit;

    /**
     * 昵称输入框
     */
    @BindView(R.id.id_edt_nickname)
    EditText mEdtNickname;

    private String mContent = "";

    @Override
    protected void initView(Bundle savedInstanceState) {
        ImmersionBar.with(this).statusBarDarkFont(true,Constant.STATUS_BAR_FONT_ALPHA).init();
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mCurrentPageId= Self.Page.app_modify_nick_name_page;

        mEdtNickname.addTextChangedListener(this);
        mContent = getIntent().getStringExtra("content");
        if (!TextUtils.isEmpty(mContent)) {
            mEdtNickname.setText(mContent);
            try {
                mEdtNickname.setSelection(mEdtNickname.getText().length());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_nickname_update;
    }

    @Override
    protected void onResume() {
        super.onResume();
        UmengEventUtils.pageEvent(mContext,
                ApplicationHelper.getApplicationContext().getString(R.string.event_0x018));
        GoogleAnalyticsUtil.setScreenName(Google.NAME_UPDATE);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        //输入框文字改变后
        int length = s.toString().trim().length();
        if (length >= 2) {
            mTvSubmit.setAlpha(1.0f);
            mTvSubmit.setEnabled(true);
        } else {
            mTvSubmit.setAlpha(0.5f);
            mTvSubmit.setEnabled(false);
        }
    }

    @OnClick(R.id.id_tv_submit)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_tv_submit://完成
                SelfUtils.buriedClickData(Self.Action.app_click_modify_nick_name,
                        Self.Action.app_click_modify_nick_name_des,
                        mEnterPageTime,
                        mCurrentPageId,
                        mPrePageId);
                Intent intent = getIntent();
                intent.putExtra("content", mEdtNickname.getText().toString().trim());
                setResult(Activity.RESULT_OK, intent);
                finish();
                GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_ORD, Google.ACTION_MODIFY_NAME);
                break;
        }
    }
}
